package com.tang.thepath;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.tang.thepath.model.RequestModel;
import com.tang.thepath.service.BizService;
import com.tang.thepath.service.HttpSubscriber;
import com.tang.thepath.service.JWTHolder;

import java.util.ArrayList;
import java.util.List;

public class RepairListActivity extends BaseActivity {

    private List<RequestModel.Repair> dataList = new ArrayList<>();
    private RepairListActivity.ListAdapter adapter;
    private ListView listView;
    private int page = 1;
    private boolean hasMore = true;
    private int pageSize = 20;
    private int lastItem = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_repair_list);

        listView = (ListView) findViewById(R.id.listView);
        adapter = new RepairListActivity.ListAdapter();

        listView.setAdapter(adapter);
        listView.setOnScrollListener(new AbsListView.OnScrollListener() {
            //AbsListView view 这个view对象就是listview
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
                    if (view.getLastVisiblePosition() == view.getCount() - 1) {
                        loadPage(page++);
                    }
                }
            }
            @Override
            public void onScroll(AbsListView view, int firstVisibleItem,
                                 int visibleItemCount, int totalItemCount) {
                lastItem = firstVisibleItem + visibleItemCount - 1 ;
            }
        });

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                RequestModel.Repair repair = dataList.get(position);
                Bundle bundle = new Bundle();
                bundle.putSerializable("repair",repair);
                Intent intent = new Intent(RepairListActivity.this,RepairDetailActivity.class);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        hasMore = true;
        dataList.clear();
        page = 1;
        this.loadPage(page++);
    }


    protected void loadPage(int page){
        if(!hasMore){
            if(dataList.size() > pageSize){
                //toast("没有更多数据了");
            }
            return;
        }
        showLoading("正在加载");
        BizService.listRepair(page).subscribe(new HttpSubscriber<RequestModel.ListRepairResponse>() {
            @Override
            protected void onSuccess(RequestModel.ListRepairResponse response) {
                if(response.repairList.size() < pageSize){
                    hasMore = false;
                }
                dismisLoading();
                dataList.addAll(response.repairList);
                adapter.notifyDataSetChanged();
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_add, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int itemId = item.getItemId();
        if(itemId == R.id.menu_add){
            JWTHolder holder = JWTHolder.getInstance();
            if(holder.getRole() == 1){
                startActivity(new Intent(getContext(), RepairActivity.class));
            }else{
                toast("您没有权限使用该功能");
            }
        }else if(itemId == android.R.id.home){
            finish();
        }
        return true;
    }


    class ListAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return dataList.size();
        }

        @Override
        public Object getItem(int i) {
            return dataList.get(i);
        }

        @Override
        public long getItemId(int i) {
            return  dataList.get(i) != null ? dataList.get(i).id : 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            RepairListActivity.ViewHolder holder = null;
            if(convertView == null){
                LayoutInflater inflater = LayoutInflater.from(getContext());
                convertView = inflater.inflate(R.layout.item_repair,null);

                holder = new RepairListActivity.ViewHolder();
                holder.tvDate = (TextView) convertView.findViewById(R.id.tv_date);
                holder.tvContact = (TextView) convertView.findViewById(R.id.tv_contact);
                holder.tvIssue = (TextView) convertView.findViewById(R.id.tv_issue);
                holder.tvReason = (TextView) convertView.findViewById(R.id.tv_reason);
                holder.tvUpname = (TextView) convertView.findViewById(R.id.tv_upname);
                convertView.setTag(holder);

            }else{
                holder = (RepairListActivity.ViewHolder)convertView.getTag();
            }

            RequestModel.Repair obj =(RequestModel.Repair) getItem(position);
            holder.tvDate.setText(obj.createdAt.substring(0,16));
            holder.tvContact.setText(obj.contact);
            holder.tvIssue.setText(obj.issueTitle);
            holder.tvReason.setText(obj.reason);
            holder.tvUpname.setText(obj.upName);
            return convertView;
        }
    }

    class ViewHolder {
        TextView tvDate;
        TextView tvContact;
        TextView tvIssue;
        TextView tvReason;
        TextView tvUpname;
    }
}
